package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.comm.ZOZOPOCODE;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.BackreceiptlistOS;
import zozopo.warehouse.operations.BackreceiptlistTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.PurchaselistOS;
import zozopo.warehouse.operations.PurchaselistTB;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;
import zozopo.warehouse.operations.WarehouseOS;
import zozopo.warehouse.operations.WarehouseTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AddBackreceiptlistAction  extends ActionSupport implements Action,SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.BACKRECEIPTLIST.ADD";
	
	//传入参数


	private int receiptlistid_backreceiptlist;
	
	
	private double shipprice_backreceiptlist = 0.00;
	

	private String remark_backreceiptlist = "";

	
	private String receiptInfo = "";
	
	//传出参数
	
	private BackreceiptlistTB  backreceiptlist;
	
	private PurchaselistTB purchaselist;
	private List<PurchaseTB> list;
	private String operationResult;
	private boolean sqlError = false;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	
	public String execute(){
		
		//权限处理
		
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 

		
		
		
	
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
	
		try {
			ReceiptlistOS receiptlistOS = new ReceiptlistOS(conn);
			ReceiptOS receiptOS = new ReceiptOS(conn);
			PurchaseOS purchaseOS = new PurchaseOS(conn);
			BackreceiptlistOS backreceiptlistOS = new BackreceiptlistOS(conn);
			
			conn.setAutoCommit(false);

			ReceiptlistTB receiptlist = receiptlistOS.getReceiptlistById(receiptlistid_backreceiptlist);
			
			if(receiptlist.getState_receiptlist().equals(ZOZOPOCODE.CANCELED)){
				addFieldError("errormessage", "出仓已经取消无法退仓!");
				databaseManager.releaseConnection(conn);
				return NONE;
			}
			
			backreceiptlist = new BackreceiptlistTB();
			
			backreceiptlist.setDepartment_backreceiptlist(receiptlist.getDepartment_receiptlist());
			backreceiptlist.setDepartmentid_backreceiptlist(receiptlist.getDepartmentid_receiptlist());
			//backreceiptlist.setGoodsnumber_backreceiptlist(goodsnumber_backreceiptlist)
			backreceiptlist.setReceiptlistid_backreceiptlist(receiptlistid_backreceiptlist);
			backreceiptlist.setRemark_backreceiptlist(remark_backreceiptlist);
			backreceiptlist.setShipprice_backreceiptlist(shipprice_backreceiptlist);
			//backreceiptlist.setTotal_backreceiptlist(total_backreceiptlist);
			backreceiptlist.setUseremail_backreceiptlist(operator.getEmail_operator());
			backreceiptlist.setUsername_backreceiptlist(operator.getName_operator());
			
			
			
			List<ReceiptTB> receiptList = receiptOS.getListByInStr(receiptInfo);
			
			 
			
			if(!backreceiptlistOS.insert(backreceiptlist)){
				sqlError = true;
			}
			
			int maxId = backreceiptlistOS.getMaxIdByEmail(operator.getEmail_operator());
			backreceiptlist = backreceiptlistOS.get(maxId);
			
			List<PurchaseTB> purchaseList = receiptOS.ReceiptListToPurchaseList(receiptList,backreceiptlist.getId_backreceiptlist());
			
			if(!purchaseOS.insert(purchaseList,true,true)){
				sqlError = true;
			}
			
			//receiptlistOS.upadteForProfit(receiptlist);
			  
			list = purchaseOS.getListByBackreceiptlistId(maxId);
			
			
			
			if(sqlError){
				throw new SQLException();
			}
			
			
			
			conn.commit();
			conn.setAutoCommit(true);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}catch (Exception e) 
		{
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			databaseManager.releaseConnection(conn);
		}
		
		return SUCCESS;//*/
		
	}
	

	
	public void validate() {
		remark_backreceiptlist = remark_backreceiptlist.replaceAll("'", "\\\\'");
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有查看退货权限.");
			return;
		}
	}
	

	public PurchaselistTB getPurchaselist() {
		return purchaselist;
	}

	public void setPurchaselist(PurchaselistTB purchaselist) {
		this.purchaselist = purchaselist;
	}

	public List<PurchaseTB> getList() {
		return list;
	}

	public void setList(List<PurchaseTB> list) {
		this.list = list;
	}

	public String getOperationResult() {
		return operationResult;
	}

	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}


	public int getReceiptlistid_backreceiptlist() {
		return receiptlistid_backreceiptlist;
	}


	public void setReceiptlistid_backreceiptlist(int receiptlistid_backreceiptlist) {
		this.receiptlistid_backreceiptlist = receiptlistid_backreceiptlist;
	}


	public double getShipprice_backreceiptlist() {
		return shipprice_backreceiptlist;
	}


	public void setShipprice_backreceiptlist(double shipprice_backreceiptlist) {
		this.shipprice_backreceiptlist = shipprice_backreceiptlist;
	}


	public String getRemark_backreceiptlist() {
		return remark_backreceiptlist;
	}


	public void setRemark_backreceiptlist(String remark_backreceiptlist) {
		this.remark_backreceiptlist = remark_backreceiptlist;
	}


	public String getReceiptInfo() {
		return receiptInfo;
	}


	public void setReceiptInfo(String receiptInfo) {
		this.receiptInfo = receiptInfo;
	}


	public BackreceiptlistTB getBackreceiptlist() {
		return backreceiptlist;
	}


	public void setBackreceiptlist(BackreceiptlistTB backreceiptlist) {
		this.backreceiptlist = backreceiptlist;
	}


	public static String getLimitedcode() {
		return limitedCode;
	}


}
